package com.fengke.cn.yejian11.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fengke.cn.yejian11.R;
import com.fengke.cn.yejian11.activity.ShaiTuInfoActivity;
import com.fengke.cn.yejian11.config.Config;
import com.fengke.cn.yejian11.javabean.ShaiTuList;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/10/3 0003.
 */

public class ShaiTuListAdapter extends RecyclerView.Adapter {

    private List<ShaiTuList> shaiTuLists;
    private Context context;
    private LayoutInflater layoutInflater;
    String club;

    public ShaiTuListAdapter(Context context,List<ShaiTuList> shaiTuLists){
        this.context=context;
        this.shaiTuLists=shaiTuLists;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView=LayoutInflater.from(context).inflate(R.layout.item_shaitulist,viewGroup,false);
        ShaiTuListViewHolder holder=new ShaiTuListViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        ShaiTuListViewHolder shaiTuListViewHolder= (ShaiTuListViewHolder) viewHolder;
        final ShaiTuList shaitu=shaiTuLists.get(position);
        final String address="http://172.24.10.175/workout/Uploads/"+shaitu.getThumb0();
        String shaitu_text=shaitu.getClub_name();
        String shaitu_hit=shaitu.getHits();
        String shaitu_introduction=shaitu.getTitle();

        if (shaitu.getShaier()==null){
             ((ShaiTuListViewHolder) viewHolder).user_name.setText("尹京华");
            ((ShaiTuListViewHolder) viewHolder).pic.setImageResource(R.drawable.dog_1);
        }else {
        final String Suser_Name=shaitu.getShaier().getUser_name();
            shaiTuListViewHolder.user_name.setText(shaitu.getShaier().getUser_name());
            if (shaitu.getShaier().getPhoto()==null){
                Picasso.with(context).load(R.drawable.dog_1).into(shaiTuListViewHolder.pic);
            }else {
                final String Suser_Pic = Config.PIC_URL + shaitu.getShaier().getPhoto();
                Picasso.with(context).load(Config.PIC_URL + shaitu.getShaier().getPhoto()).into(shaiTuListViewHolder.pic);
            }
        }
        final String Suser_Time=shaitu.getUpdate_time();
        final String Suser_ClubName=shaitu.getClub_name();
        final String Suser_Title=shaitu.getTitle();
        final String Suser_address="http://172.24.10.175/workout/Uploads/"+shaitu.getThumb0();
        final String Suser_address1="http://172.24.10.175/workout/Uploads/"+shaitu.getThumb1();
        final String Suser_address2="http://172.24.10.175/workout/Uploads/"+shaitu.getThumb2();

        shaiTuListViewHolder.shaitu_hit.setText(shaitu_hit);
        if(Suser_ClubName==null) {
            shaiTuListViewHolder.shaitu_text.setText("射箭俱乐部");
            club="射箭俱乐部";
        }else {
            shaiTuListViewHolder.shaitu_text.setText(shaitu_text);
            club=Suser_ClubName;
        }
        shaiTuListViewHolder.shaitu_introduction.setText(shaitu_introduction);
        if (!shaitu.getThumb0().equals("")&&!shaitu.getThumb0().equals("a")){
            Picasso.with(context).load(address).into(shaiTuListViewHolder.shaitu_pic);
        }else {
            Picasso.with(context).load(R.drawable.dog_1).into(shaiTuListViewHolder.shaitu_pic);
        }

        ////////////////////////////////点击事件
        shaiTuListViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(context, ShaiTuInfoActivity.class);
//                intent.putExtra("Suser_Name",);
//                intent.putExtra("Suser_Pic",Suser_Pic);
//                intent.putExtra("Suser_Time",Suser_Time);
                intent.putExtra("Suser_ClubName",club);
//                intent.putExtra("Suser_Title",Suser_Title);
//                intent.putExtra("Suser_address",Suser_address);
//                intent.putExtra("Suser_address1",Suser_address1);
//                intent.putExtra("Suser_address2",Suser_address2);
                intent.putExtra("ShaiTu_Id",shaitu.getId());
                context.startActivity(intent);//启动下一个Activity
            }
        });
    }

    @Override
    public int getItemCount() {
        return shaiTuLists.size();
    }

    public class ShaiTuListViewHolder extends RecyclerView.ViewHolder{

        private View itemView;
        private ImageView shaitu_pic;
        private TextView shaitu_text;
        private TextView shaitu_hit;
        private TextView shaitu_introduction;
        TextView user_name;
        ImageView pic;
        public ShaiTuListViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            user_name=(TextView) itemView.findViewById(R.id.user_name);
            pic=(ImageView)itemView.findViewById(R.id.user_pic) ;
            shaitu_pic= (ImageView) itemView.findViewById(R.id.item_shaitupic);
            shaitu_text= (TextView) itemView.findViewById(R.id.item_shaitutext);
            shaitu_hit= (TextView) itemView.findViewById(R.id.item_shaituhit);
            shaitu_introduction= (TextView) itemView.findViewById(R.id.item_shaituintroduction);
        }
    }
}
